package com.uparlange.project.flextris
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;

	/**
	 * Music player
	 * 
	 * @author Ugo Parlange
	 */ 
	public class JukeBox
	{
		/**
		 * Musics, Sound effects
		 */ 
		[Embed(source="/com/uparlange/project/flextris/music/music_home.mp3")]
		private static var TITLE_SCREEN : Class;
		[Embed(source="/com/uparlange/project/flextris/music/music_a.mp3")]
		private static var MUSIC_A : Class;
		[Embed(source="/com/uparlange/project/flextris/music/music_b.mp3")]
		private static var MUSIC_B : Class;
		[Embed(source="/com/uparlange/project/flextris/music/music_c.mp3")]
		private static var MUSIC_C : Class;
		[Embed(source="/com/uparlange/project/flextris/music/game_over.mp3")]
		private static var GAME_OVER : Class;
		[Embed(source="/com/uparlange/project/flextris/music/music_score.mp3")]
		private static var SCORE : Class;
		[Embed(source="/com/uparlange/project/flextris/music/move.mp3")]
		private static var MOVE_BLOCK : Class;
		[Embed(source="/com/uparlange/project/flextris/music/rotate.mp3")]
		private static var ROTATE_BLOCK : Class;
		[Embed(source="/com/uparlange/project/flextris/music/level_up.mp3")]
		private static var LEVEL_UP : Class;
		[Embed(source="/com/uparlange/project/flextris/music/line_complete.mp3")]
		private static var LINE_COMPLETE : Class;
		[Embed(source="/com/uparlange/project/flextris/music/finish_drop.mp3")]
		private static var FINISH_DROP : Class;
		
		/**
		 * Default music volume
		 */ 
		private static const MUSIC_VOLUME : Number = 0.2;
		
		/**
		 * unique instance of the Jukebox
		 */ 
		private static var instance : JukeBox;
		
		/**
		 * gets the unique Jukebox instance
		 * 
		 * @return JukeBox, the unique Jukebox instance
		 */ 
		public static function getInstance() : JukeBox {
			if(instance == null) {
				instance = new JukeBox(new JukeBoxSingleton());
			}
			return instance;
		}
		
		/**
		 * Title screen sound
		 */ 
		private var titleScreenSound : Sound;
		
		/**
		 * Music A sound
		 */ 
		private var musicASound : Sound;
		
		/**
		 * Music B sound
		 */
		private var musicBSound : Sound;
		
		/**
		 * Music C sound
		 */
		private var musicCSound : Sound;
		
		/**
		 * Game Over sound
		 */
		private var gameOverSound : Sound;
		
		/**
		 * Score sound
		 */
		private var scoreSound : Sound;
		
		/**
		 * Move block sound
		 */ 
		private var moveBlockSound : Sound;
		
		/**
		 * Rotate block sound
		 */ 
		private var rotateBlockSound : Sound;
		
		/**
		 * Level up sound
		 */ 
		private var levelUpSound : Sound;
		
		/**
		 * Finish drop sound
		 */ 
		private var finishDropSound : Sound;
		
		/**
		 * Line complete sound
		 */ 
		private var lineCompleteSound : Sound;
		
		/**
		 * Current playing music
		 */ 
		private var currentMusic : Sound;
		private var currentMusicChannel : SoundChannel;
		
		/**
		 * Constructor
		 * 
		 * @param aJukeBoxSingleton JukeBoxSingleton, the Jukebox singleton class
		 */ 
		public function JukeBox(aJukeBoxSingleton : JukeBoxSingleton) {
			super();
			init();
		}
		
		private function init() : void {
			titleScreenSound = new TITLE_SCREEN() as Sound;
			musicASound = new MUSIC_A() as Sound;
			musicBSound = new MUSIC_B() as Sound;
			musicCSound = new MUSIC_C() as Sound;
			gameOverSound = new GAME_OVER() as Sound;
			moveBlockSound = new MOVE_BLOCK() as Sound;
			rotateBlockSound = new ROTATE_BLOCK() as Sound;
			levelUpSound = new LEVEL_UP() as Sound;
			finishDropSound = new FINISH_DROP() as Sound;
			lineCompleteSound = new LINE_COMPLETE() as Sound;
			scoreSound = new SCORE() as Sound;
		}
		
		/**
		 * Play level up sound
		 */ 
		public function playLevelUp() : void {
			levelUpSound.play();
		}
		
		/**
		 * Play line complete sound
		 */
		public function playLineComplete() : void {
			lineCompleteSound.play();
		}
		
		/**
		 * Play move block sound
		 */
		public function playMoveBlock() : void {
			moveBlockSound.play();
		}
		
		/**
		 * Play rotate block sound
		 */
		public function playRotateBlock() : void {
			rotateBlockSound.play();
		}
		
		/**
		 * Play finish drop sound
		 */
		public function playFinisDropSound() : void {
			finishDropSound.play();
		}
		
		/**
		 * Play title screen sound
		 */
		public function playTitleScreen() : void {
			playMusic(titleScreenSound);
		}
		
		/**
		 * Play music A sound
		 */
		public function playMusicA() : void {
			playMusic(musicASound);
		}
		
		/**
		 * Play music B sound
		 */
		public function playMusicB() : void {
			playMusic(musicBSound);
		}
		
		/**
		 * Play music C sound
		 */
		public function playMusicC() : void {
			playMusic(musicCSound);
		}
		
		/**
		 * Play game over sound
		 */
		public function playGameOver() : void {
			gameOverSound.play();
		}
		
		/**
		 * Play score sound
		 */
		public function playScore() : void {
			playMusic(scoreSound);
		}
		
		/**
		 * Stop current music
		 */
		public function stopMusic() : void {
			playMusic(null);
		}
		
		/**
		 * Play / pause current music
		 */ 
		public function playPauseMusic() : void {
			if(currentMusicChannel != null) {
				var currentSoundTransform : SoundTransform = currentMusicChannel.soundTransform;
				var volume : Number = (currentSoundTransform.volume == MUSIC_VOLUME ? 0 : MUSIC_VOLUME);
				currentMusicChannel.soundTransform = new SoundTransform(volume);
			}
		}
		
		/**
		 * Play a music
		 * 
		 * @param aMusic Sound, the music to play
		 */ 
		private function playMusic(aMusic : Sound) : void {
			if(currentMusicChannel != null) {
				currentMusicChannel.stop();
			}
			currentMusic = aMusic;
			if(currentMusic != null) {
				currentMusicChannel = currentMusic.play(0, int.MAX_VALUE, new SoundTransform(MUSIC_VOLUME));
			}
		}
	}
}
class JukeBoxSingleton {}